﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace HisGL.Net.Core
{
  public class CryptoHelper
  {
    /// <summary>  
    /// AES加密算法  
    /// </summary>  
    /// <param name="input">明文字符串</param>  
    /// <returns>字符串</returns>  
    public static string EncryptByAES(string input, string sKey)
    {
      if (sKey.Length != 16)
      {
        throw new Exception("Key长度必须为16位");
      }
      if (string.IsNullOrWhiteSpace(input))
      {
        return input;
      }
      string plainText = input;

      byte[] encrypted;

      // Create an Aes object
      // with the specified key and IV.
      using (Aes aesAlg = Aes.Create())
      {
        aesAlg.Key = Encoding.UTF8.GetBytes(sKey);
        aesAlg.IV = Encoding.UTF8.GetBytes(sKey);

        // Create an encryptor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
          using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
          {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {
              //Write all data to the stream.
              swEncrypt.Write(plainText);
            }
            encrypted = msEncrypt.ToArray();
          }
        }
      }

      // Return the encrypted bytes from the memory stream.
      return Convert.ToBase64String(encrypted);
    }

    /// <summary>  
    /// AES解密  
    /// </summary>  
    /// <param name="input">密文字节数组</param>  
    /// <returns>返回解密后的字符串</returns>  
    public static string DecryptByAES(string input, string sKey)
    {
      if (sKey.Length != 16)
      {
        throw new Exception("Key长度必须为16位");
      }

      if (string.IsNullOrWhiteSpace(input))
      {
        return input;
      }

      var buffer = Convert.FromBase64String(input);
      // Declare the string used to hold
      // the decrypted text.
      string plaintext = null;

      // Create an Aes object
      // with the specified key and IV.
      using (Aes aesAlg = Aes.Create())
      {
        aesAlg.Key = Encoding.UTF8.GetBytes(sKey);
        aesAlg.IV = Encoding.UTF8.GetBytes(sKey);

        // Create a decryptor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(buffer))
        {
          using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
          {
            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
            {

              // Read the decrypted bytes from the decrypting stream
              // and place them in a string.
              plaintext = srDecrypt.ReadToEnd();
            }
          }
        }
      }

      return plaintext;
    }

    /// <summary>
    /// MD5
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string MD5Encrypt32(string text)
    {
      string cl = text;
      string pwd = "";
      MD5 md5 = MD5.Create();
      byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
      for (int i = 0; i < s.Length; i++)
      {
        pwd = pwd + s[i].ToString("X2");
      }
      return pwd;
    }

    public static string SHA256Encrypt(string text)
    {
      string cl = text;
      string pwd = "";
      SHA256 sha = SHA256.Create();
      byte[] s = sha.ComputeHash(Encoding.UTF8.GetBytes(cl));
      for (int i = 0; i < s.Length; i++)
      {
        pwd = pwd + s[i].ToString("X2");
      }
      return pwd;
    }



    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="sCodeType"></param>
    /// <param name="sText"></param>
    /// <returns></returns>
    public static string EncodeBase64(string sCodeType, string sText)
    {
      string encode = "";
      byte[] bytes = Encoding.GetEncoding(sCodeType).GetBytes(sText);
      try
      {
        encode = Convert.ToBase64String(bytes);
      }
      catch
      {
        encode = sText;
      }
      return encode;
    }

    /// <summary>
    /// 解码
    /// </summary>
    /// <param name="sCodeType"></param>
    /// <param name="sText"></param>
    /// <returns></returns>
    public static string DecodeBase64(string sCodeType, string sText)
    {
      string decode = "";
      byte[] bytes = Convert.FromBase64String(sText);
      try
      {
        decode = Encoding.GetEncoding(sCodeType).GetString(bytes);
      }
      catch
      {
        decode = sText;
      }
      return decode;
    }

    public static string EncodeBase64(string sText)
    {
      return EncodeBase64("GB2312", sText); //utf-8
    }

    public static string DecodeBase64(string sText)
    {
      return DecodeBase64("GB2312", sText);
    }

  }
}
